
#include <common.h>
#include <command.h>
#include <net.h>
#include <phy.h>
#include <miiphy.h>
__attribute__((aligned(32))) static const unsigned char send_packet[1024] = {0};
unsigned long int next = 1;
unsigned int  test_rand(void){
	next = next * 1103515245 + 12345;
	return (unsigned int)(next/65535)%32768;
}

void test_srand(unsigned long int seed){
	next = seed;
}

void construct_tx_frame(unsigned char *buf, int size, int data){
	int i = 0;

	if(data != 0xff){
		test_srand(data);
		printf("set random data, buf add is %x\n",(int)buf);
		while(i < size){
			memset(buf++, test_rand() , 1);
			i++;
		}
	}
	else{
		printf("set all 0xff\n");
		memset(buf, 0xff , size);
	}
}

int cmd_gmac_send_frame(cmd_tbl_t *cmdtp,int flag,int argc,char *argv[]) {
	#if(1)
	int speed;
	int duplex;
	int loop_size;
	int data;
	char *endp;
	char *buf_send = send_packet;
	int i;
	struct eth_device * p_eth = 0;
	struct phy_device *p_phy_dev = 0;

	if (argc != 5) {
		cmd_usage(cmdtp);
		return -1;
	}
	printf("send frame with %s\n",eth_get_name());
	p_eth = eth_get_dev_by_name(eth_get_name());
	if(!p_eth){
		printf("find no eth dev...\n");
		return -1;
	}

	p_phy_dev = mdio_phydev_for_ethname(eth_get_name());
	if (!p_phy_dev){
		printf("find no phy dev...\n");
		return -1;
	}

	speed = simple_strtoul(argv[1], &endp, 0);
	duplex = simple_strtoul(argv[2], &endp, 0);
	loop_size = simple_strtoul(argv[3], &endp, 0);
	data = simple_strtoul(argv[4], &endp, 16);
	printf("%s%s : %d : %x\n",speed == 1 ? "100M" : "10M", duplex == 1 ? "_duplex_full" : "_duplex_half", loop_size,data);
	construct_tx_frame(send_packet,sizeof(send_packet),data);

	p_phy_dev->autoneg = AUTONEG_DISABLE;
	p_phy_dev->duplex = (duplex == 1 ? DUPLEX_FULL : DUPLEX_HALF);
	p_phy_dev->speed = (speed == 1 ? SPEED_100 : SPEED_10);

	p_eth->init(p_eth, 0);

	while(loop_size--){
		p_eth->send(p_eth, send_packet, sizeof(send_packet));
	}
	printf("send done...\n");
	#endif

}


U_BOOT_CMD(
	cmd_gmac_send_frame,5,1,cmd_gmac_send_frame,"usage:gmac send  frame\n",
	" speed dulex loop_times send_data (abort: ctl + c)\n"

);

